package com.test.ws.s1.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.test.ws.s1.controller")/// 链接控制层controller
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
    /**
            * 创建视图解析器
* @return
        */
    @Bean(name = "viewResolver")
    public ViewResolver viewResolverInit(){
        InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/");//设置前缀
        viewResolver.setSuffix(".jsp");//设置后缀
        return viewResolver;
    }
    /**
     * 启用默认静态资源处理
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    //启用静态处理
        configurer.enable();
    }


    //DataSource
    @Bean
    public DataSource getDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//        dataSource.setUrl("jdbc:mysql://172.16.6.59:3306/2403db?useSSL=false");
        dataSource.setUrl("jdbc:mysql://localhost:3306/1201b?useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }


    //QueryRunner
    @Bean
    public QueryRunner queryRunner(){
        return new QueryRunner(getDataSource());
    }

    //文件传输配置  方法名是multipartResolver 或者bean中配置名字multipartResolver
    @Bean("multipartResolver")
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(100000000);
        //设置上传文件大小最大值
        return multipartResolver;
    }

}
